﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ThreeByte.Media;
using System.Xml.Linq;
using System.Reflection;
using ThreeByte.ActiveDeck.Config;
using log4net;
using System.ComponentModel;

namespace ThreeByte.ActiveDeck.AddIn.Content.Controls
{
    public class DesignerControl : INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DesignerControl));

        protected static readonly XNamespace ns = Service.ServiceConfiguration.NAMESPACE;

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected DesignerControl() {
            //Must take care of all important initialization in derived classes
        }

        public DesignerControl(PixelSpace defaultPixelSpace) {
            DefaultPixelSpace = defaultPixelSpace;
            CurrentPixelSpace = defaultPixelSpace;
        }

        void resetPosition_Click(object sender, System.Windows.RoutedEventArgs e) {
            ResetPosition();
        }


        private string _condition;
        public string Condition{
            get {
                return _condition;
            }
            set {
                _condition = value;
                NotifyPropertyChanged("Condition");
            }
        }


        [Browsable(false)]
        public PixelSpace DefaultPixelSpace {
            get;
            protected set;
        }

        [Browsable(false)]
        public PixelSpace CurrentPixelSpace {
            get;
            set;
        }

        public void ResetPosition() {
            CurrentPixelSpace.X = DefaultPixelSpace.X;
            CurrentPixelSpace.Y = DefaultPixelSpace.Y;
            CurrentPixelSpace.W = DefaultPixelSpace.W;
            CurrentPixelSpace.H = DefaultPixelSpace.H;
        }

        public virtual XElement ToXml() {
            XElement node = new XElement(ns + "DesignerControl");

            node.Add(new XAttribute("Type", this.GetType()));
            if(!string.IsNullOrWhiteSpace(Condition)) {
                node.Add(new XAttribute("Condition", Condition));
            }
            return node;
        }

        public virtual XElement GetPresentationXml(PixelTranslator translation = null) {
            return ToXml(); //Meant to be overridden in derived classes
        }

        public virtual List<VideoResource> GetMediaResources() {
            return new List<VideoResource>(); //Empty - Meant to be override in derived classes
        }

        public virtual List<GraphicResource> GetGraphicResources() {
            return new List<GraphicResource>(); //Empty - Meant to be override in derived classes
        }

        public static DesignerControl FromXml(XElement node, Dictionary<Guid, GraphicResource> resources) {

            Type newControlType = Type.GetType(node.Attribute("Type").Value, true);
            ConstructorInfo ci = newControlType.GetConstructor(new Type[] { typeof(XElement), typeof(Dictionary<Guid, GraphicResource>) });
            DesignerControl newControl = (DesignerControl)ci.Invoke(new object[] { node, resources });

            XAttribute condAtt = node.Attribute("Condition");
            if(condAtt != null) {
                newControl.Condition = condAtt.Value;
            }
            return newControl;
        }

        protected static object[] PixelSpaceAsAttributes(PixelSpace pos) {
            List<object> atts = new List<object>();
            atts.Add(new XAttribute("Top", pos.Y));
            atts.Add(new XAttribute("Left", pos.X));
            atts.Add(new XAttribute("Width", pos.W));
            atts.Add(new XAttribute("Height", pos.H));
           
            return atts.ToArray();
        }
    }
}
